<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\CwArticle;
use app\models\CwCarousel;
use app\models\CwPanel;
 use yii\data\Pagination;


class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        $model = CwCarousel::find()->limit(3)->orderBy(['car_id' => SORT_DESC])->all();
        $model2 = CwPanel::find()->limit(9)->orderBy(['pan_id' => SORT_DESC])->all();
        return $this->render('index',['carousel' => $model, 'panel' => $model2]);
    }

    public function actionView($id){
        return $this->render('page',['model' => $this->findModel($id)]);
    }
    
    public function actionTpl1($id){
        return $this->render('tpl1',['model' => \app\models\CwTpl1Article::findOne($id)]);
    }

    public function actionTpl2(){
        $model = \app\models\CwTpl2Article::find();
        $count = $model->count();
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => 15]);
        $list = $model->offset($pager->offset)->limit($pager->limit)->orderBy(['ta2_id' => SORT_DESC])->all();
        return $this->render("tpl2",['list' => $list, 'pager' => $pager]);
    }
    
    public function actionTpl2p($id){
        return $this->render('tpl2p',['model' => \app\models\CwTpl2Article::findOne($id)]);
    }

    protected function findModel($id)
    {
        if (($model = CwArticle::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
